package codingforgreat.lchot150;

public class Lc395 {
    public int longestSubstring(String s, int k) {
        char[] str = s.toCharArray();
        int N = str.length;
        int max = 0;
        for (int i = 0; i < N; i++) {
            int[] count = new int[256];
            int collect = 0;
            int satisfy = 0;
            for (int j = i; j < N; j++) {
                if (count[str[j]] == 0) {
                    collect++;
                }
                if (count[str[j]] == k - 1) {
                    satisfy++;
                }
                count[str[j]]++;
                if (collect == satisfy) {
                    max = Math.max(max, j - i + 1);
                }
            }
        }
        return max;
    }
}
